<?php
namespace jy_h5game;
defined('BY_JYA') or exit('error');
/*
	工具类
*/
class model extends \plugin{
	public function is_shopgame($id,$exi=false){
		$shopgameid=pdo_getcolumn('jy_weishop_game',array('game_id'=>$id),'id');
		$wait=pdo_get('jy_weishop_game_wait',array('status'=>0,'game_id'=>$shopgameid,'mid'=>$_SESSION['uid']));
		if($wait['id']){
			return $wait['id'];
		}else{
			if($exi && $shopgameid){
				exi('该游戏已经参与商城活动，只能通过商城来参与！','error','core/home.welcome/');
			}elseif($shopgameid){
				return 'isshop';
			}else{
				return 0;
			}
		}
	}
	public function up_shopgame($id){
		pdo_update('jy_weishop_game_wait',array('status'=>1),array('id'=>$id));
		return $shopgameid;
	}
	public function get_lasttimes($item,$waitid=0){
		if($waitid){
			if($waitid=='isshop'){
				$leftPlayCount=0;
			}else{
				$game_id=pdo_getcolumn('jy_weishop_game_wait',array('id'=>$waitid),'game_id');
				$leftPlayCount=pdo_count('jy_weishop_game_wait',array('status'=>0,'game_id'=>$game_id,'mid'=>$_SESSION['uid']),'id');
			}
		}else{
			if($item['isLimitDrawTimes']== 1){
				$leftPlayCount = $item['playTotalLimit']-$item['istotalnum'];
				if($leftPlayCount< 0){
					$leftPlayCount= 0;
				}
			}else{
				$leftPlayCount='无限';
			}
		}		
		return $leftPlayCount;
	}
	public function get_checkgame($item,$waitid=0){
		if($waitid){
			$leftPlayCount=$this->get_lasttimes($item,$waitid);
			if($leftPlayCount<=0){
				$jsondata['errorCode'] = 10998;
				$jsondata['errorMsg'] = '亲，本抽奖活动你只剩0次名额！';
				return $jsondata;
			}
		}
		//查询玩家中奖次数
		$awardTimes = pdo_count('jy_h5game_award',array('rid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'fansID'=>$_SESSION['uid']));
		//Z玩家在该活动中的信息
		$isInList = pdo_get('jy_h5game_fans',array('fansID'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$item['id']));
		if($item['gameTypeSetting'] == 1 ){
			if(time() <= $item['starttime'] || $item['status'] == 1){
				$jsondata['errorCode'] = 10998;
				$jsondata['errorMsg'] = '亲，本次抽奖活动将于'.$item['starttime2'].'开启,敬请期待！';
			}
			elseif(time() >= $item['endtime'] || $item['status'] == 3) {
				$jsondata['errorCode'] = 10998;
				$jsondata['errorMsg'] = '亲，本次抽奖活动已结束，请关注我们的下一次活动，谢谢～';
			}
			 //每人中奖次数限制
			elseif($item['awardTimesLimit']>0 && $item['awardTimesLimit'] <= $awardTimes && !$waitid){
				 $jsondata['errorCode'] = 10998;
				 $jsondata['errorMsg'] = '亲，本次抽奖活动最多允许中奖'.$item['awardTimesLimit'].'次，您已经中奖'.$awardTimes.'次';
				
			}//抽奖次数限制
			elseif($item['playnum']>0&&$isInList['totalnum'] >= $item['playnum'] && !$waitid){
				$jsondata['errorCode'] = 10998;
				$jsondata['errorMsg'] = '亲，本次抽奖活动最多允许参加'.$item['playnum'].'次，您已经参加'.$isInList['totalnum'].'次'; 
			}else{
				$jsondata['errorCode'] = 0;
				$jsondata['errorMsg'] = '123';
			}		
		}elseif($item['gameTypeSetting'] == 2){
			$jsondata['errorCode'] = 0;
			$jsondata['errorMsg'] = '123';	
		}else{
			$jsondata['errorCode'] = 0;
			$jsondata['errorMsg'] = '123';
		}
		return $jsondata;
	}
	//检查游戏权限
	public function check_game($waitid=0,$shopgameid=0,$exi=false){
		if($shopgameid){
			$wait=pdo_get('jy_weishop_game_wait',array('status'=>0,'game_id'=>$shopgameid,'mid'=>$_SESSION['uid']));
			$game_id=pdo_getcolumn('jy_weishop_game',array('id'=>$shopgameid),'game_id');
			if($wait['game_id']){
				return $game_id;
			}else{
				if($exi){
					exi('你暂无参与该游戏的名额！','error','jy_h5game/notice//id='.$game_id);
				}
			}
		}else{
			$wait=pdo_get('jy_weishop_game_wait',array('id'=>$waitid,'mid'=>$_SESSION['uid']));
			if($wait['game_id']){
				$game_id=pdo_getcolumn('jy_weishop_game',array('id'=>$wait['game_id']),'game_id');
				if($exi && $wait['status']){
					exi('你暂无参与该游戏的名额！','error','jy_h5game/notice//id='.$game_id);
				}else{
					return $game_id;
				}
			}else{
				if($exi){
					exi('没找到该游戏！','error','core/home.welcome/');
				}
			}
		}		
		return $game_id;
	}
	//参与兑奖
	public function get_award($id,$noexi=false,$post=array()){
		if(!$id){
			if($noexi){
				return false;
			}else{
				exi('参数错误！','error');
			}
		}
		$award=pdo_get('jy_h5game_award',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($award['awardstatus']==1){
			if($noexi){
				return false;
			}else{
				exi('您已领取过了，请勿重复领取！','error');
			}
		}
		$award['awardvalue']=json_decode($award['awardvalue'],true);
		//币奖励
		if($award['prizeType']>=2 && $award['prizeType']<=6){
			$awardstatus=2;
			$value=$award['awardvalue']['jifen']?:0;
			$credittype=$award['prizeType']==2?1:$award['prizeType'];
			cfc('core')->member_credit($award['fansID'],'credit'.$credittype,$value,1,'jy_h5game','参与活动奖励');
			$this->model('model')->sethexiao($id,$post);
		}else{
			$awardstatus=1;
		}
		$update=array(
			'awardstatus'=>$awardstatus,
			'ausername'=>$post['ausername'],
			'aphone'=>$post['aphone'],
			'address'=>$post['address'],
		);
		pdo_update('jy_h5game_award',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	//插入核销记录
	public function sethexiao($id,$post=array(),$consumeType=0){
		$award=pdo_get('jy_h5game_award',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'rid' => $award['rid'],
			'mid' => $award['fansID'],
			'awardid' => $award['id'],
			'awardinfo'=>$post['content'],
			'consummer' => '',
			'code1'=>$award['code1'],
			'note'=>$award['note'],
			'express'=>$post['express'],
			'expresssn'=>$post['expresssn'],
			'ausername'=>$post['ausername'],
			'aphone'=>$post['aphone'],
			'address'=>$post['address'],
			'consumeType'=>$consumeType,
			'createtime' => time(),
		);
		$this->insert('hexiao',$data);
		pdo_update('jy_h5game_award',array('awardstatus'=>2),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
}